package com.hj.ipproxy.bean;

import com.hj.ipproxy.httpclient.HttpClientManager;
import com.hj.ipproxy.httpclient.MethodType;
import com.hj.ipproxy.httpclient.TcpType;
import com.hj.ipproxy.util.ProxyIpUtil;
import com.hj.tools.StringUtil;

/**
 * 请求响应封闭类
 * @author HJ
 */
public class HttpEntity {
	private String url;
	private String body;
	private String ip;
	private int port;
	private String addrs;
	private String userAgent = ProxyIpUtil.getFireFoxUA();
	private MethodType method = MethodType.GET;
	private long respStart = 0l;
	private long respEnd = 0l;
	private int connecttimerout = 3000;
	private int readtimeout = 3000;
	private String charset = "UTF-8";
	private TcpType tcpType = TcpType.GET_HTTP;
	
	public HttpEntity(){
	}

	public HttpEntity(String url){
		this.url = url;
	}
	
	public HttpEntity(String url, String ip, int port){
		this.url = url;
		this.ip = ip;
		this.port = port;
	}
	
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getAddrs() {
		return addrs;
	}
	public void setAddrs(String addrs) {
		this.addrs = addrs;
	}
	public String getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	public long getRespStart() {
		return respStart;
	}
	
	public long getRespEnd() {
		return respEnd;
	}
	
	public String getCharset() {
		return charset;
	}
	public void setCharset(String charset) {
		this.charset = charset;
	}
	
	public long getRespTime() {
		return respEnd - respStart;
	}
	
	public void setStarttime(){
		respStart = System.currentTimeMillis();
	}
	
	public void setEndtime(){
		respEnd = System.currentTimeMillis();
	}
	
	/**
	 * 发送请求
	 */
	public boolean send(){
		if (MethodType.GET == method) {
			HttpClientManager.doGet(this);
		}
		return isSuccess();
	}
	

	public String getUserAgent() {
		return userAgent;
	}

	public void setUserAgent(String userAgent) {
		this.userAgent = userAgent;
	}

	public int getConnecttimerout() {
		return connecttimerout;
	}

	public void setConnecttimerout(int connecttimerout) {
		this.connecttimerout = connecttimerout;
	}

	public int getReadtimeout() {
		return readtimeout;
	}

	public void setReadtimeout(int readtimeout) {
		this.readtimeout = readtimeout;
	}
	
	public boolean isSuccess(){
		return StringUtil.isNotEmpty(body);
	}

	public TcpType getTcpType() {
		return tcpType;
	}

	public void setTcpType(TcpType tcpType) {
		this.tcpType = tcpType;
	}

	public MethodType getMethod() {
		return method;
	}

	public void setMethod(MethodType method) {
		this.method = method;
	}
	
	@Override
	public String toString() {
		return ip+":"+port;
	}
}
